
#include <p32xxxx.h>
#include "board.h"

#ifdef LCD_DEBUG
#include "lcdText.h"
#endif

#define LCD_DEBUG
#define UART_DEBUG


void consoleInit()
{
    U3MODEbits.ON = 1;      // Enable UART
    U3STAbits.UTXEN = 1;    // Enable Transmit
    U3BRG = 42;             // (80MHz / 16 x 115200) - 1
}

// _mon_putc is used by stdio functions e.g. printf
void _mon_putc (char c)
{
    while (U3STAbits.UTXBF);
    U3TXREG = c;
}

void delayMs(unsigned int msec)
{
    unsigned int tWait;
    unsigned int tStart;

    tWait = (SYS_FREQ/2000) * msec;
    
    tStart = ReadCoreTimer();

    while((ReadCoreTimer()-tStart)<tWait);
}

//-------------------------------
// Usage: delayUs(15);
// delays the processor by 15uS
// AM: I have written this extremely quickly and havent tested it thoroughly.
//-------------------------------
void delayUs(unsigned int uSec)
{
    unsigned int delay = SYS_FREQ/4/4/1000000;  // estimation: 4 cycles per instruction & 4 instructions per loop (1 for compare, 1 for increment, 2 for goto)
    unsigned int counter = 0;
    for(counter = 0; counter < delay; counter++);

}

